package AI;

import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Collections;
import FrameWork.PathFinding.Map;
import FrameWork.PathFinding.NodeMap;
import FrameWork.PathFinding.NodeMap.Path;
import FrameWork.PathFinding.Step;
import Items.Item;
import Main.SpaceGame;
import Tasks.MineAsteroids;
import Tasks.Task;
import Tasks.UrgencyComparator;

public abstract class AiEntity implements Map
{
	protected int x;
	protected int y;
	public int destinationX, destinationY;
	public Path currentPath;
	private NodeMap nodeMap;
	protected double speed = .5;
	protected int stepCnt = 0;
	public ArrayList<Task> taskList = new ArrayList<Task>();
	protected Task currentTask = null;
	public double health;
	public ArrayList<Item> inventory = new ArrayList<Item>();
	
	protected SpaceGame main;
	
	public AiEntity(int x,int y,SpaceGame main, int health)
	{
		setPos(x,y);
		this.main = main;
		nodeMap = new NodeMap(this);
		this.health = health;
		currentTask = null;
	}
	
	public void step()
	{
		if(currentPath != null)
		{
			if(stepCnt >= speed*100)
			{
				
				if(currentPath != null && !currentPath.isFinished()&& currentPath.checkCurrentPath()){
					//System.out.println(currentPath.nextStep());
					Step step = currentPath.nextStep();
					setPos(step.getX(), step.getY());
				}else{
					calculatePath();
				}
				stepCnt = 0;
			}
			else
				stepCnt++;
		}
	}
	
	public void updateTask()
	{
		{
			if(currentTask == null)
			{
				if(taskList.size() <= 0)
				{ 
					for(int i = 0;i<main.taskManager.taskList.size();i++)
					{
						Collections.sort(main.taskManager.taskList,new UrgencyComparator());
						taskList.add(main.taskManager.taskList.get(i));
						main.taskManager.taskList.remove(main.taskManager.taskList.get(i));
					}
				}
				if(taskList.size()>0)
				{
					Collections.sort(taskList,new UrgencyComparator());
					currentTask = taskList.get(0);
					main.taskManager.taskList.remove(currentTask);
				}
			}
			else
			{
				if(currentTask.isFinished(this))
				{
					taskList.remove(currentTask);
					currentTask = null;
				}
				if(currentTask != null)
				{
					checkForBetterTask(currentTask);
					currentTask.performTask(this);
				}
			}
		}
	}
	
	public void setDestination(int x,int y)
	{
		destinationX = x;
		destinationY = y;
		calculatePath();
	}
	
	protected void calculatePath()
	{
		nodeMap.clear();
		currentPath = nodeMap.run(x, y, destinationX, destinationY, 10000);
	}
	
	public void setPos(int x, int y)
	{
		this.x = x;
		this.y = y;
	}
	
	public abstract void draw(Graphics g);
	
	public int getX()
	{
		return x;
	}
	
	public int getY()
	{
		return y;
	}
	
	public void checkForBetterTask(Task currentTask)
	{
		boolean hasBetterTask = false;
		for(int i = 0; i<taskList.size();i++)
		{
			if (currentTask != null && taskList.get(i).urgency > currentTask.urgency)
			{
				Collections.sort(taskList,new UrgencyComparator());
				if (currentTask instanceof MineAsteroids)
				{
					((MineAsteroids) currentTask).m.setActive(false);
				}
				currentTask.finished = true;
				taskList.remove(currentTask);
				currentTask = null;
				hasBetterTask = true;
			}
		}
		if(!hasBetterTask)
		{
			for(int i = 0;i<main.taskManager.taskList.size();i++)
			{
				if(main.taskManager.taskList.get(i).urgency > currentTask.urgency)
				{
					Collections.sort(main.taskManager.taskList, new UrgencyComparator());
					if (currentTask instanceof MineAsteroids)
					{
						((MineAsteroids) currentTask).m.setActive(false);
					}
					currentTask.finished = true;
					taskList.remove(currentTask);
					currentTask = main.taskManager.taskList.get(i);
					main.taskManager.taskList.remove(i);
				}
			}
		}
	}
	
	public void update()
	{
		if(main.world.tiles[x][y].getOxygen() < 50 && health > 0)
		{
			health -= .05;
		}
		else if(health <= 0)
			main.aiManager.kill(this);
	}
	
	public void pickUpItem(Item i)
	{
		main.itemManager.itemList.remove(i);
		inventory.add(i);
		i.beingPickedUp = false;
	}
	
	public void dropItem(Item i)
	{
		main.itemManager.itemList.add(i);
		inventory.remove(i);
		i.x = x;
		i.y = y;
	}
	
	public boolean canReachPos(int x,int y,int max)
	{
		return nodeMap.hasNode(x, y);
	}
	
	public void attack(AiEntity e)
	{
		e.health-=Math.random()*5;
	}
}
